<section class="content-header">
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">文章管理</a></li>
        <li class="active">标签管理</li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="jax-box">
        <form id="formSearch" class="form-horizontal form-search">
            <div class="form-group">
                <div class="col-md-4 col-sm-6">
                    <label class="control-label  col-xs-4" for="name">名称:</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" id="name">
                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <label class="control-label  col-xs-4" for="description">描述:</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" id="description">
                    </div>
                </div>
                <shiro:hasPermission name="tag:list">
                    <div class="col-md-1 col-sm-6 " style="text-align:left;">
                        <button type="button" id="btn_query" class="btn btn-info">查询</button>
                    </div>
                </shiro:hasPermission>
            </div>
        </form>
    </div>
    <div class="jax-box jax-box-table">
        <div id="toolbar" class="btn-group">
            <shiro:hasPermission name="tag:add">
            <button id="btn_add" type="button" class="btn btn-primary" data-toggle="modal" data-target="#tagModal">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
            </button>
            </shiro:hasPermission>
            <shiro:hasPermission name="tag:batchDelete">
            <button id="btn_batch_delete" type="button" class="btn btn-danger">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量删除
            </button>
            </shiro:hasPermission>
        </div>
        <table id="table"></table>
    </div>
</section>
<!-- 标签新增模态框 -->
<div id="tagModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog"  role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">新增标签</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="tagForm">
                    <input  name="origin" type="hidden" value="1">
                    <div class="box-body">
                        <div class="form-group">
                            <label  class="col-sm-3 control-label">标签名称 <span class="red">*</span>:</label>
                            <div class="col-sm-8">
                                <input  name="name" class="form-control" require="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label  class="col-sm-3 control-label">标签描述 <span class="red">*</span>:</label>
                            <div class="col-sm-8">
                                <input name="description" class="form-control" require="">
                            </div>
                        </div>
                    </div>
                    <div class="box-footer">
                        <div class="pull-right">
                            <button type="button" id="saveTag" class="btn btn-info">确定</button>
                            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!-- 标签详情模态框 -->
<div id="tagDetailModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">标签信息</h4>
            </div>
            <div class="modal-body">
                <div id="tagOpenWindow">
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    var editFlag = "[[${@perms.hasPerm('tag:edit')}]]";
    var deleteFlag = "[[${@perms.hasPerm('tag:delete')}]]";
    var columns = [
        {checkbox: true },
        {
            field: 'name',
            title: '标签名称',
            align : "center"
        },{
            field: 'description',
            title: '标签描述',
            align : "center",
        },
        {
            field : 'operation',
            title : '操作',
            align : "center",
            class:'min-width-100',
            formatter : function(value,
                                 row, index) {
                var edit = editFlag=="true" ? '<a class="table-btn table-btn-info" onclick="editTag('+row.id+')">编辑</a>' : '';
                var del = deleteFlag=="true" ? '<a  class="table-btn table-btn-danger" onclick="deleteTag('+row.id+')">删除</a>' : '';
                return edit + del;
            }
        }];
    var options={
        id:"#table",
        url: '/tag/list',
        columns:columns,
        toolbar: '#toolbar',
        showRefresh: true,
        queryParams : queryParams
    }
    Core.initTable(options);

    /*查询参数*/
    function queryParams(params) {
        var temp = { //这里的键的名字和控制器的变量名必须一致，这边改动，控制器也需要改成一样的
            limit : params.limit, //页面大小
            offset : params.offset, //页码
            name : $("#name").val(),
            description : $("#description").val()
        };
        return temp;
    }

    /*编辑*/
    function editTag(id) {
        Core.load("#tagOpenWindow","/tag/edit?id="+id,function(){
            $("#tagDetailModal").modal("show");
        },2);
    }
    /*删除*/
    function deleteTag(id) {
        Core.confirm("确定删除该标签？",function () {
            Core.postAjax("/tag/delete",{"id":id},function (data) {
                if(data.status==200){
                    Core.refreshTable("#table");
                }
                layer.msg(data.msg);
            })
        })
    }
    $(function () {
        /*查询*/
        $("#btn_query").click(function(){
            Core.refreshTable("#table");
        });
        /*新增*/
        $("#btn_add").click(function(){
            Core.clearError("#tagForm");
        });
        /*批量删除*/
        $("#btn_batch_delete").click(function(){
            var checkedRows= Core.selectMutiData("#table");
            if(checkedRows){
                Core.confirm("确定删除选中的"+checkedRows.length+"条记录？",function () {
                    var ids=[];
                    $.each(checkedRows, function (i, item) {
                        ids.push(item.id);
                    })
                    Core.postAjax("/tag/batch/delete", {"ids": ids}, function (data) {
                        if (data.status == 200) {
                            Core.refreshTable("#table");
                        }
                        layer.msg(data.msg);
                    })
                })
            }
        });

        /*保存*/
        $("#saveTag").click(function(){
            if(doValidForm("#tagForm")){
                Core.mask("#saveTag");
                Core.postAjax("/tag/add",$("#tagForm").serialize(),function (data) {
                    Core.unmask("#saveTag");
                    if(data.status==200){
                        $("#tagModal").modal("hide");
                        $("#tagForm")[0].reset();
                        Core.refreshTable("#table");
                    }
                    layer.msg(data.msg);
                })
            };
        });
    });

</script>